package cn.ahxb.frame.work.ui;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.widget.VideoView;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ahxb.frame.R;
import cn.ahxb.frame.common.base.BaseActivity;
import cn.ahxb.frame.common.widgets.CountdownCircleView;
import cn.ahxb.frame.work.ui.home.activities.MainActivity;


public class SplashActivity extends BaseActivity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {

    @BindView(R.id.ccv_count_down)
    CountdownCircleView mCircleView;

    @BindView(R.id.vv_splash)
    VideoView mVideoView;

    //页面跳转延迟时长
    private static final int VALUE_DELAYED_TIME = 3;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        File video = new File(Environment.getExternalStorageDirectory(), "gg.mp4");
        if (video.exists()) {
            String path = video.getAbsolutePath();
            mVideoView.setVideoPath(path);
            mVideoView.setOnCompletionListener(this);
            mVideoView.setOnPreparedListener(this);
            mVideoView.start();
        }else {
            mCircleView.start(VALUE_DELAYED_TIME, new CountdownCircleView.OnTimeChangeListener() {
                @Override
                public void onTimeOver() {
                    startMainActivity();
                }
            });
        }
    }

    /**
     * 启动主页面
     */
    @OnClick(R.id.ccv_count_down)
    void startMainActivity() {
        if (mVideoView.isPlaying()) {
            mVideoView.pause();
        }
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
//        startMainActivity();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        int duration = mp.getDuration() / 1000;
        mCircleView.start(duration == 0 ? 1 : duration, new CountdownCircleView.OnTimeChangeListener() {
            @Override
            public void onTimeOver() {
                startMainActivity();
            }
        });
    }
}
